//
//  ViewController.m
//  CompareDemo
//
//  Created by 向祖华 on 16/4/27.
//  Copyright © 2016年 向祖华. All rights reserved.
//

#import "ViewController.h"
#import "Student.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    /*
     知识点：
     1、不可变数组的排序
     - (NSArray *)sortedArrayUsingSelector:(SEL)comparator;
     - (NSArray *)sortedArrayUsingComparator:(NSComparator)cmptr;
     2、可变数组的排序
     - (void)sortUsingSelector:(SEL)comparator;
     - (void)sortUsingComparator:(NSComparator)cmptr;
     3、字典的排序
     - (NSArray *)keysSortedByValueUsingComparator:(NSComparator)cmptr
     - (NSArray *)keysSortedByValueUsingSelector:(SEL)comparator;
     */
    
    //1、简单不可变数组的排序
    //第一种方法selector方法选择器
    NSArray *imArr = @[@"aa",@"rr",@"pp",@"hh",@"xx",@"vv"];
    NSArray *sortArr = [imArr sortedArrayUsingSelector:@selector(compare:)];
    NSLog(@"简单不可变数组的排序第一种方法%@",sortArr);
    //第二种方法block方法
    [imArr sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
        return [(NSString *)obj1 compare:(NSString *)obj2];
    }];
    NSLog(@"简单不可变数组的排序第二种方法%@",imArr);

    //2.简单可变数组的排序
    //第一种：方法选择器
    NSMutableArray *mArr = [@[@54 ,@33,@12,@23,@65] mutableCopy];
    [mArr sortUsingSelector:@selector(compare:)];//compare数组中两个元素比较的方法
    NSLog(@"简单可变数组的排序第一种方法%@",mArr);
    //第二种：block方法
    [mArr sortUsingComparator:^NSComparisonResult(id obj1, id obj2) {
        return [(NSNumber *)obj1 compare:(NSNumber *)obj2];
    }];
    NSLog(@"简单可变数组的排序第二中方法%@",mArr);
    //3.字典的排序：原理同数组，将字典中的键通过方法
    NSDictionary * numsDic = @{@(2):@"second",
                               @(1):@"first",
                               @(3):@"thrid"};
    NSDictionary * strDic = @{@"id_1":@"first",
                              @"id_3":@"thrid",
                              @"id_2":@"second"};
    //排序后得到的是值排序后的数组
    NSArray * numsSortedKeys = [numsDic keysSortedByValueUsingSelector:@selector(compare:)];
    NSArray * strSortedKyes = [strDic keysSortedByValueUsingComparator:^NSComparisonResult(id obj1, id obj2) {
        NSString * str1 = obj1;
        NSString * str2 = obj2;
        return [str1 compare:str2];
    }];
    NSLog(@"字典排序的第一种方法%@",numsSortedKeys.description);
    NSLog(@"字典排序的第二种方法%@",strSortedKyes.description);
    
    //4.复杂数组的排序
    Student *student1 = [Student studentWithName:@"a" age:23 score:21 number:3434343];
    Student *student2 = [Student studentWithName:@"b" age:45 score:432.4 number:324];
    Student *student3 = [Student studentWithName:@"c" age:32 score:4321.4 number:2343];
    Student *student4 = [Student studentWithName:@"d" age:7 score:43.4 number:233];
    Student *student5 = [Student studentWithName:@"e" age:73 score:65 number:2332424];
    
    NSMutableArray *arr = [NSMutableArray arrayWithObjects:
                           student1,student2,student3,student4,student5,nil];
    
    //按照年龄升序排序
    [arr sortUsingSelector:@selector(compareByAge:)];
    NSLog(@"%@",arr);
    //按照成绩降序排序
    [arr sortUsingSelector:@selector(compareByScore:)];
    NSLog(@"%@",arr);
    //按照姓名降序排序
    [arr sortUsingSelector:@selector(compareByName:)];
    NSLog(@"%@",arr);
    //按照学号升序排序
    [arr sortUsingSelector:@selector(compareByNumber:)];
    NSLog(@"%@",arr);
}






- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
